//
//  HQNewVenderViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/17.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQNewVenderViewController.h"

@interface HQNewVenderViewController ()

@end

@implementation HQNewVenderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"合作厂家";
    [self requestList];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQVenderCell *cell = [HQVenderCell cellWithTableView:tableView];
    cell.dataInfo = _dataSource[indexPath.row];
    [cell.callPhone addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];
    [cell.editButton addTarget:self action:@selector(editItem:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

/**
 拨打电话
 
 @param button 按钮
 */
-(void)callPhone:(HQButton *)button{
    NSString *phoneNo =  button.dataInfo[@"phone"];
    NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNo];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
    [self.view addSubview:callWebview];
}

/**
 点击每条的编辑按钮
 */
-(void)editItem:(HQButton *)button{
    NSString *message = [NSString stringWithFormat:@"确认要与【%@】解除合作关系?",button.dataInfo[@"shop_name"]];
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
    [cancelAction setValue:[self colorHexValue:0x233040] forKey:@"titleTextColor"];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认解除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam deletshop:[UserModelManager userID] other_id:button.dataInfo[@"other_id"]] withMethod:[HQMethod deletshop] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                [self tostView:@"解除合作关系成功"];
                [self requestList];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }];
    [photoAction setValue:[self colorHexValue:0x233040] forKey:@"titleTextColor"];
    
    // 添加响应方式
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:photoAction];
    // 显示
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

/**
 获取合作厂家列表
 */
-(void)requestList{
    [self showHudText:@""];
    _tableView.backgroundView = nil;
    [HQNetWork requetParam:[HQParam listshop:[UserModelManager userID]] withMethod:[HQMethod listshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            
            if(_dataSource.count ==0){
                [self addNoneDataView:_tableView withText:@"暂无数据"];
            }
            [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}


@end
